package com.ctrip.ec.api.admin.controller.notice;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.message.MessageDao;
import com.ctrip.ec.biz.entity.message.MessageEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.notice.NoticeDao;
import com.ctrip.ec.biz.entity.notice.NoticeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@RestController
@RequestMapping("notice")
public class NoticeController{
    @Autowired
    private NoticeDao notice;

    @Autowired
    private MessageDao message;

    @PostMapping("list")
    public List<NoticeEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<NoticeEntity> list = notice.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  NoticeEntity detail(@PathVariable Long id) throws SQLException {
        return notice.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody NoticeEntity entity) throws SQLException {
        notice.save(entity);

        //向message表发送一条消息
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setContent(entity.getContent());
        messageEntity.setType(0);
        messageEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        messageEntity.setContentId(0L);
        message.save(messageEntity);
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        notice.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        notice.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        notice.unerase(ids);
    }
}
